package com.jrzn.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;


/**
 * Created by Administrator on 2015/6/19.
 */
public abstract class Adapter<T> extends BaseAdapter {
	protected Activity mactivity;
	protected List<T> mlist;
	private int mlayout;
	private Handler handler;

	public Handler getHandler() {
		return handler;
	}

	public Adapter(Activity activity, List<T> list, int layout) {
		this.mactivity = activity;
		this.mlist = list;
		this.mlayout = layout;
	}

	public Adapter(Activity activity, List<T> list, int layout,
			Handler handler) {
		// TODO Auto-generated constructor stub
		this(activity, list, layout);
		this.handler = handler;
	}

	@Override
	public int getCount() {
		return mlist != null ? mlist.size() : 0;
	}

	@Override
	public Object getItem(int arg0) {
		return mlist != null ? mlist.get(arg0) : null;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ViewHolder vh = ViewHolder.get(mactivity, arg1, arg2, mlayout, arg0);
		getview(vh, arg0, mlist.get(arg0));
		return vh.getConvertView();
	}

	public abstract void getview(ViewHolder vh, int position, T T);

	public void add(T T) {
		if (T != null) {
			mlist.add(T);
			notifyDataSetChanged();
		}
	}

	public void addAll(List<T> list) {
		if (list != null && list.size() != 0) {
			mlist.addAll(list);
			notifyDataSetChanged();
		}
	}

	public void remove(T T) {
		if (T != null) {
			mlist.remove(T);
			notifyDataSetChanged();
		}
	}

	public List<T> getList() {
		return mlist;
	}

	public void setMlist(List<T> list) {
		this.mlist = list;
		notifyDataSetChanged();
	}

	public void clear() {
		mlist.clear();
		notifyDataSetChanged();
	}

	public void setDeviceList(ArrayList<T> list) {
		if (list != null) {
			mlist = (ArrayList<T>) list.clone();
			notifyDataSetChanged();
		}
	}

	public void clearDeviceList() {
		if (mlist != null) {
			mlist.clear();
		}
		notifyDataSetChanged();
	}

}